package cn.zhangjie.pet.controller;

import cn.zhangjie.basic.util.JsonResult;
import cn.zhangjie.basic.util.PageList;
import cn.zhangjie.pet.domain.PetType;
import cn.zhangjie.pet.query.PetTypeQuery;
import cn.zhangjie.pet.service.IPetTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/petType")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class PetTypeController {
    @Autowired
    private IPetTypeService petTypeService;
    @GetMapping("typeTree")
    @ApiOperation(value = "查询所有" )
    public List<PetType> typeTree(){
        return petTypeService.typeTree();
    }
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<PetType> findall(){
        return petTypeService.findAll();
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条" )
    public PetType findone(@PathVariable("id") Long id){
        return petTypeService.findById(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )
    public JsonResult delete(@PathVariable("id") Long id){

        try {
            petTypeService.del(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    public JsonResult updataoradd(@RequestBody PetType petType){

        try {
            if(petType.getId()==null){
                petTypeService.add(petType);
                return JsonResult.me();
            }else {
                petTypeService.update(petType);
                return JsonResult.me();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<PetType> pagequery(@RequestBody PetTypeQuery petTypeQuery){
        try {
            return petTypeService.queryPage(petTypeQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public JsonResult pactchdel(@RequestBody List<Long> ids){

        try {
            petTypeService.pactchdel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("批量删除失败");
        }
    }







}
